package com.zx.ishopping.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 生成订单号
 */

@Component
public class OrderNumberUtils {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    public String getOrderNumber(){
        String data = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        //设置redis key
        String key = "order:number:" + data;
        //redis自增
        Long number = redisTemplate.opsForValue().increment(key);

        if (number != null && number == 1L) {
            redisTemplate.expire(key, java.time.Duration.ofDays(2));

            
        }

        return data + String.format("%08d", number);
    }
    
    
}
